package com.design.fitment.util;

import com.design.fitment.app.MyApplication;
import com.design.fitment.constant.URLs;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.Map;
import java.util.Set;

/**
 * Created by jerry on 15/11/2.
 */
public class DesignNetworkUtil
{
    private static DesignNetworkUtil util = new DesignNetworkUtil();
    private HttpUtils httpUtils;

    private DesignNetworkUtil()
    {
        httpUtils =MyApplication.getApp().getHttpUtils();
    }

    public static DesignNetworkUtil getInstance()
    {
        return util;
    }


    public void getBannaer(RequestDataCallback callback)
    {
        doGet(URLs.MAIN_PAGE, null, callback);
    }
    public void getCasesDetail(RequestDataCallback callback,int casesId)
    {
        doGet("http://www.shejiben.com/mobile/index.php?id="+casesId+"&uid=0&action=detail&module=cases&", null, callback);
    }
    public void getJieXiDetail(RequestDataCallback callback,int casesId)
    {
        doGet("http://www.shejiben.com/mobile/index.php?id="+casesId+"&uid=0&action=jiexiDetail&module=cases&&", null, callback);
    }
    //http://www.shejiben.com/mobile/index.php?pageSize=30&uid=0&action=home&module=cases&page=1&
    public void getMoreList(RequestDataCallback callback,String action)
    {
        doGet("http://www.shejiben.com/mobile/index.php?pageSize=30&uid=0&action="+action+"&module=cases&page=1&", null, callback);
    }
    public void getGalleryHome(RequestDataCallback callback)
    {
        doGet(URLs.GalleryHome_LIST, null, callback);
    }
    public void getGalleryPublic(RequestDataCallback callback)
    {
        doGet(URLs.GalleryPublic_LIST, null, callback);
    }


    public void getAnswer(RequestDataCallback callback)
    {
        doGet(URLs.Answer, null, callback);
    }

    public void getDynamic(RequestDataCallback callback)
    {
        doGet(URLs.Dynamic, null, callback);
    }
    public void getNewest(RequestDataCallback callback)
    {
        doGet(URLs.Newest, null, callback);
    }




    private void doGet(String requestUrl,Map<String,Object> params,final RequestDataCallback callback)
    {
        RequestParams requestParams = new RequestParams();

        if (params != null)
        {
            Set<Map.Entry<String,Object>> set = params.entrySet();

            for (Map.Entry<String,Object> entry : set)
            {
                requestParams.addQueryStringParameter(
                        entry.getKey(),String.valueOf(entry.getValue()));
            }
        }

        httpUtils.send(HttpRequest.HttpMethod.GET,
                requestUrl,requestParams ,
                new RequestCallBack<String>()
                {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo)
                    {
                        callback.onSuccess(responseInfo.result);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg)
                    {
                        callback.onFailure(error);
                    }
                });
    }

    private void doPost(String requestUrl,Map<String,Object> params,final RequestDataCallback callback)
    {
        RequestParams requestParams = new RequestParams();

        if (params != null)
        {
            Set<Map.Entry<String,Object>> set = params.entrySet();

            for (Map.Entry<String,Object> entry : set)
            {
                requestParams.addBodyParameter(
                        entry.getKey(), String.valueOf(entry.getValue()));
            }
        }

        httpUtils.send(HttpRequest.HttpMethod.POST,
                requestUrl,requestParams ,
                new RequestCallBack<String>()
                {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo)
                    {
                        callback.onSuccess(responseInfo.result);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg)
                    {
                        callback.onFailure(error);
                    }
                });
    }


}
